package com.infotel.test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JComponent;
import javax.swing.JWindow;
import javax.swing.Timer;

import com.infotel.listener.TimerListener;

public class Horloge extends JComponent {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    GregorianCalendar heure = null;
    int diametreSeconde = 100;
    int diametreMinute = 90;   
    int diametreHeure = 80 ;
    int espacement = 15;

    public int getDiametreSeconde() {
        return diametreSeconde;
    }

    public void setDiametreSeconde(int diametreSeconde) {
        this.diametreSeconde = diametreSeconde;
    }



    public Horloge() {

        TimerListener timerListener = new TimerListener(this);
        Timer timer = new Timer(1000, timerListener);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics gi) {
        diametreMinute = 90 * diametreSeconde / 100;
        diametreHeure = 80 * diametreSeconde / 100;
        Graphics2D g = (Graphics2D)gi;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        heure = new GregorianCalendar();
        heure.setTime(new Date());
        //g.setBackground(new Color(0,true));
        //g.setColor(Color.WHITE);
       // g.fillRect(0, 0, getWidth(), getHeight());
        g.setStroke(new BasicStroke(2));
     g.setColor(new Color(240, 240, 255,125));

        g.fillOval(espacement, espacement, diametreSeconde * 2,
                diametreSeconde * 2);
        g.setColor(Color.BLACK);

        g.drawOval(espacement, espacement, diametreSeconde * 2, diametreSeconde * 2);
//        g.drawString(String.valueOf(heure.get(GregorianCalendar.HOUR)) + ":"
//                + String.valueOf(heure.get(GregorianCalendar.MINUTE)) + ":"
//                + String.valueOf(heure.get(GregorianCalendar.SECOND)),
//                diametreSeconde + espacement, diametreSeconde + espacement);

        for (int i = 1; i < 13; i++) {
            double alphaNumero = (-Math.PI / 6.0) * i + (Math.PI / 2);
            int xNumero = (int) Math.round(Math.cos(alphaNumero)
                    * (diametreSeconde-8 ) + diametreSeconde + 10);
            int yNumero = (int) Math.round(-Math.sin(alphaNumero)
                    * (diametreSeconde-8 ) + diametreSeconde + 20);
            g.drawString(String.valueOf(i), xNumero, yNumero);
        }
        g.setStroke(new BasicStroke(1));
        double alphaSeconde = (-Math.PI / 30.0)
                * heure.get(GregorianCalendar.SECOND) + (Math.PI / 2);
        int xSeconde = (int) Math.round(Math.cos(alphaSeconde)
                * diametreSeconde + diametreSeconde + espacement);
        int ySeconde = (int) Math.round(-Math.sin(alphaSeconde)
                * diametreSeconde + diametreSeconde + espacement);
        g.drawLine(diametreSeconde + espacement, diametreSeconde + espacement,
                xSeconde, ySeconde);
        g.setStroke(new BasicStroke(1.5f));
        double alphaMinute = (-Math.PI / 30.0)
                * heure.get(GregorianCalendar.MINUTE) + (Math.PI / 2)- Math.abs((alphaSeconde-Math.PI/2)/60);
        int xMinute = (int) Math.round(Math.cos(alphaMinute) * diametreMinute
                + diametreSeconde + espacement);
        int yMinute = (int) Math.round(-Math.sin(alphaMinute) * diametreMinute
                + diametreSeconde + espacement);
        g.drawLine(diametreSeconde + espacement, diametreSeconde + espacement,
                xMinute, yMinute);

        double alphaHeure = (-Math.PI / 6.0)
                * heure.get(GregorianCalendar.HOUR) + (Math.PI / 2) - Math.abs((alphaMinute-Math.PI/2)/12);
        int xHeure = (int) Math.round(Math.cos(alphaHeure) * diametreHeure
                + diametreSeconde + espacement);
        int yHeure = (int) Math.round(-Math.sin(alphaHeure) * diametreHeure
                + diametreSeconde + espacement);
        g.setColor(Color.RED);
        g.setStroke(new BasicStroke(2.5f));
        g.drawLine(diametreSeconde + espacement, diametreSeconde + espacement,
                xHeure, yHeure);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(diametreSeconde * 2 + espacement*2, diametreSeconde
                * 2 + espacement*2);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        JWindow fenetre = new JWindow();
        final Horloge horloge = new Horloge();
        
        fenetre.add(horloge);
        fenetre.pack();
        //fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fenetre.setVisible(true);
        fenetre.setBackground(new Color(0,true));
        fenetre.addComponentListener(new ComponentListener() {
            
            @Override
            public void componentShown(ComponentEvent arg0) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void componentResized(ComponentEvent arg0) {
                int size =0;
                if (arg0.getComponent().getWidth()>arg0.getComponent().getHeight()){
                    size= arg0.getComponent().getHeight()-60;
                }else{
                    size= arg0.getComponent().getWidth()-30;
                }
                horloge.setDiametreSeconde(size/2);
                
            }
            
            @Override
            public void componentMoved(ComponentEvent arg0) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void componentHidden(ComponentEvent arg0) {
                // TODO Auto-generated method stub
                
            }
        });

    }

}
